import com.fasterxml.jackson.databind.ObjectMapper;
import com.stein.utils.EsClient;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;

import java.io.IOException;
import java.util.Map;

public class TremsTest01 {

    //json转换工具对象
    private ObjectMapper mapper = new ObjectMapper();

    //es连接
    private RestHighLevelClient client = EsClient.getClient();

    //索引名字
    private String index = "book";

    /**
     *term查询
     * @throws Exception
     */
    @Test
    public void term() throws Exception{
        SearchRequest searchRequest = new SearchRequest(index);

        SearchSourceBuilder builder = new SearchSourceBuilder();

        //分页信息
        builder.from(0);
        builder.size(10);

        //查询条件
        builder.query(QueryBuilders.termQuery("desc","讲述"));

        //将查询条件放入请求体
        searchRequest.source(builder);

        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);

        for (SearchHit hit : response.getHits().getHits()) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println(sourceAsMap);
        }
    }

    /**
     *terms查询
     * @throws Exception
     */
    @Test
    public void terms() throws Exception{
        SearchRequest searchRequest = new SearchRequest(index);

        SearchSourceBuilder builder = new SearchSourceBuilder();

        //分页信息
        builder.from(0);
        builder.size(20);

        //查询条件
        builder.query(QueryBuilders.termsQuery("desc","讲述","一个"));

        //将查询条件放入请求体
        searchRequest.source(builder);

        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);

        System.out.println(response.getHits().getHits().length);

        for (SearchHit hit : response.getHits().getHits()) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println(sourceAsMap);
        }
    }
    /**
     *matchAll查询
     * @throws Exception
     */
    @Test
    public void matchAll() throws Exception{
        SearchRequest searchRequest = new SearchRequest(index);

        SearchSourceBuilder builder = new SearchSourceBuilder();

        //分页信息
        builder.from(0);
        builder.size(20);

        //查询条件
        builder.query(QueryBuilders.matchAllQuery());

        //将查询条件放入请求体
        searchRequest.source(builder);

        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);

        System.out.println(response.getHits().getHits().length);

        for (SearchHit hit : response.getHits().getHits()) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println(sourceAsMap);
        }
    }

    /**
     *match查询
     * @throws Exception
     */
    @Test
    public void match() throws Exception{
        SearchRequest searchRequest = new SearchRequest(index);

        SearchSourceBuilder builder = new SearchSourceBuilder();

        //分页信息
        builder.from(0);
        builder.size(20);

        //查询条件
        builder.query(QueryBuilders.matchQuery("desc","一"));

        //将查询条件放入请求体
        searchRequest.source(builder);

        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);

        System.out.println(response.getHits().getHits().length);

        for (SearchHit hit : response.getHits().getHits()) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println(sourceAsMap);
        }
    }


    /**
     *布尔match查询  多条件查询
     * @throws Exception
     */
    @Test
    public void boolMatch() throws Exception{
        SearchRequest searchRequest = new SearchRequest(index);

        SearchSourceBuilder builder = new SearchSourceBuilder();

        //分页信息
        builder.from(0);
        builder.size(20);

        //查询条件
        builder.query(QueryBuilders.matchQuery("desc","一 古城").operator(Operator.OR));

        //将查询条件放入请求体
        searchRequest.source(builder);

        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);

        System.out.println(response.getHits().getHits().length);

        for (SearchHit hit : response.getHits().getHits()) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println(sourceAsMap);
        }
    }
    /**
     *MultiMatch查询  多条件查询
     * @throws Exception
     */
    @Test
    public void MultiMatch() throws Exception{
        SearchRequest searchRequest = new SearchRequest(index);

        SearchSourceBuilder builder = new SearchSourceBuilder();

        //分页信息
        builder.from(0);
        builder.size(20);

        //查询条件
        builder.query(QueryBuilders.multiMatchQuery("一个","desc","name"));

        //将查询条件放入请求体
        searchRequest.source(builder);

        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);

        System.out.println(response.getHits().getHits().length);

        for (SearchHit hit : response.getHits().getHits()) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println(sourceAsMap);
        }
    }

    /**
     * 根据id查询
     * @throws IOException
     */
    @Test
    public void testFindById() throws IOException {
        //1. 创建GetRequest
        GetRequest request = new GetRequest(index,"1");

        //2. 执行查询
        GetResponse resp = client.get(request, RequestOptions.DEFAULT);

        //3. 输出结果
        System.out.println(resp.getSourceAsMap());
    }


    /**
     * 多Id查询
     */
    @Test
    public void ids() throws Exception{
        SearchRequest request = new SearchRequest(index);
        SearchSourceBuilder builder = new SearchSourceBuilder();

        builder.query(QueryBuilders.idsQuery().addIds("1","2","3"));


        request.source(builder);

        SearchResponse search = client.search(request, RequestOptions.DEFAULT);

        for (SearchHit hit : search.getHits().getHits()) {
            System.out.println(hit.getSourceAsMap());
        }


    }
}
